# -- coding: utf-8 --
# @time :
# @author : 周梦泽
# @file : .py
# @software: pycharm
from DrissionPage import WebPage
from DrissionPage.chromium_tab import ChromiumTab
from DrissionPage.errors import TabClosedError
from websocket import WebSocketBadStatusException

from common.logger.log import log_
from common.utils import mouseUtil, web_page_util
from common.utils.web_page_util import action_lock, Tap, MockTap
import time
import threading


def close_ad(params):
    """
    关闭广告小组件
    :param params: 被修饰的方法的参数映射
    :return:
    """
    page = params.get("page")
    if page is None:
        page = params.get('self').page
        if page is None:
            return
    # 支持多标签页
    try:
        # targets = [page.get_tab(tab) for tab in page.tabs]
        # for target in targets:
        target = page
        sycm_index_pop(page)
        tb_slider(page)
        survey_satisfaction_closed(page)
    except TabClosedError:
        log_.warning("关闭广告 出现 TabClosedError 异常")
    except WebSocketBadStatusException:
        log_.warning("关闭广告 出现 WebSocketBadStatusException 异常")
    except RuntimeError:
        log_.warning("浏览器已关闭或链接已断开")


def tb_slider(p: [WebPage, ChromiumTab], timeout=0) -> bool:
    """
    淘宝滑块
    :param p: 网页对象
    :param timeout: 元素超时时间
    :return: 触发滑块并成功返回 True
    """
    lock = False
    try:
        slipper_box = p("x://div[@id='nocaptcha']", timeout=timeout)
        if slipper_box and slipper_box.states.is_displayed:
            action_lock.acquire()
            log_.info(f"检测到淘宝滑块 url: {p.url}")
            slipper = slipper_box.ele("#nc_1_n1z", timeout=0)
            if slipper and slipper.states.is_displayed:
                x, y = slipper.locations.screen_midpoint
                px, py = slipper_box.locations.screen_location
                [p_w, p_h] = slipper_box.size
                [e_w, e_h] = slipper.size
                lock = True
                mouseUtil.drag((x, y), (x + p_w - e_w, y))
                return True
    finally:
        if lock:
            action_lock.release()
    return False


def survey_satisfaction_closed(page: [WebPage, ChromiumTab]):
    """
    关闭生意参谋满意度调查
    :param page:
    :return:
    """
    lock = False
    try:
        if page("x://div[contains(@class, 'aes-survey-hanging aes-survey-hanging__normal')]", timeout=0):
            satisfaction_choice = page("x://div[contains(@class, 'svelte-1oxiijx face5')]", timeout=0)
            checkbox_ele = page("x://div[contains(@class, 'aes-survey--checkbox')]", timeout=0)
            submit_button = page("x://div[contains(@class, 'aes-survey-button')]", timeout=0)
            MockTap(ele=satisfaction_choice, msg='点击复选框失败', before_delay=(1, 3))(page)
            MockTap(ele=checkbox_ele, msg='点击复选框失败', before_delay=(1, 3))(page)
            MockTap(ele=submit_button, msg='点击关闭失败', before_delay=(1, 3))(page)
            log_.info('关闭生意参谋满意度调查成功')
    finally:
        if lock:
            action_lock.release()


def sycm_index_pop(page: [WebPage, ChromiumTab]):
    lock = False
    try:
        e = page("@class:node_modules-@ali-op-ebase-lib-Notice-ImageTips-module-dsImageTipsWrapper", timeout=0)
        if e:
            e = e("t:span", timeout=0)
            if e:
                Tap(e, by_js=True)()
    finally:
        if lock:
            action_lock.release()


if __name__ == '__main__':
    p = web_page_util.create_page()
    tb_slider(p)
